package com.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.util.ResultMsg;
import com.travel.entity.TbView;
import com.travel.mapper.ViewMapper;
import com.travel.service.ViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ViewServiceImpl extends ServiceImpl<ViewMapper, TbView> implements ViewService {


    @Autowired
     ViewMapper viewMapper;

    //查询所有的景点
    @Override
    public ResultMsg findAll(int current, int size) {
        //分页
        Page<TbView> pages = new Page<>(current,size);
        Page<TbView> page = super.page(pages);
        return ResultMsg.success(page);
    }
    //按照城市查询该城市的所有景点
    @Override
    public ResultMsg findAllCity(int cityId) {
        TbView tbView = viewMapper.selectById(cityId);
        if (tbView!=null){
            return ResultMsg.success(tbView);
        }
        return ResultMsg.error(1001,"数据异常");
    }

    //按照城市的名称查询所有的景点
    @Override
    public ResultMsg findAllCityName(String cityName) {
        QueryWrapper<TbView> wrapper = new QueryWrapper<>();
        wrapper.eq("view_addr",cityName);
        List<TbView> tbViews = viewMapper.selectList(wrapper);
        if (tbViews!=null){
            return ResultMsg.success(tbViews);
        }
        return ResultMsg.error(1001,"数据异常");
    }

    //按照价格降价排序某城市的所有景点
    @Override
    public ResultMsg findDesc() {

       List<TbView> tbViews =  viewMapper.findDesc();
        if (tbViews!=null){
            return ResultMsg.success(tbViews);
        }
        return ResultMsg.error(1001,"数据异常");
    }
    //按照价格升价排序某城市的所有景点
    @Override
    public ResultMsg viewAsc() {

        List<TbView> tbViews =  viewMapper.viewAsc();
        if (tbViews!=null){
            return ResultMsg.success(tbViews);
        }
        return ResultMsg.error(1001,"数据异常");
    }
    //查询某城市所有的景点名称
    @Override
    public ResultMsg findName(String cityName) {
        List<TbView> tbViews = viewMapper.findName(cityName);
        if (tbViews!=null){
            return ResultMsg.success(tbViews);
        }
        return ResultMsg.error(1001,"数据异常");
    }
    //根据景点名查询所有的景点
    @Override
    public ResultMsg viewCityNameAndVname(String cname, String vname) {
        QueryWrapper<TbView> wrapper = new QueryWrapper<>();
        wrapper.eq("view_addr",cname);
        wrapper.eq("view_name",vname);
        List<TbView> list = super.list(wrapper);
        if (list!=null && list.size()>0){
            return ResultMsg.success(list);
        }
        return ResultMsg.error(1001,"数据异常");
    }

    //根据景点的Id查一个景点
    @Override
    public ResultMsg viewFindId(Integer viewId) {
        TbView tbView = super.getById(viewId);
        if (tbView!=null){
            return ResultMsg.success(tbView);
        }
        return ResultMsg.error(1001,"数据异常");
    }

    @Override
    public ResultMsg addOne(TbView tbView) {
        boolean save = super.save(tbView);
        if (save)
            return ResultMsg.success();
        return ResultMsg.error(1001,"数据异常");
    }

    @Override
    public ResultMsg deleteOne(Integer viewId) {
        boolean removeById = super.removeById(viewId);
        if (removeById){
            return ResultMsg.success(200);
        }
        return ResultMsg.error(303);
    }

    @Override
    public ResultMsg updateOne(TbView tbView) {
        boolean updateById = super.updateById(tbView);
        if (updateById){
            return ResultMsg.success(200);
        }
        return ResultMsg.error(300,"修改失败");
    }

}
